#ifndef JSETTINGS_H
#define JSETTINGS_H

#include <QTECore/qteglobal.h>
#include <QVariant>
#include <QObject>

namespace QTE
{

#if QT_VERSION < QT_VERSION_CHECK(5,10,0)
# ifdef Q_NO_TEXTCODEC
#  define __NO_TEXTCODEC
# endif
#elif QT_VERSION < QT_VERSION_CHECK(6,0,0)
# if !QT_CONFIG(textcodec)
#  define __NO_TEXTCODEC
# endif
#else
# define __NO_TEXTCODEC
#endif

class JSettingsPrivate;

class QTECORE_EXPORT JSettings : public QObject
{
	Q_OBJECT

public:
	explicit JSettings(const QString &group, QObject *parent = nullptr);
	~JSettings();

public:
	void setGroup(const QString &group);
	QString group() const;

public:
	void write(const QString &key, const QVariant &value);
	QVariant read(const QString &key, const QVariant &defaultValue = QVariant());

public:
	template <typename T> inline
	void write(const QString &key, T &&value);

	template <typename T> inline
	T read(const QString &key, T defaultValue = T());

public:
	void setReadFromFile(bool enable = true);
	bool isReadFromFile() const;

Q_SIGNALS:
	void written(const QString &key, const QVariant &current, const QVariant &previous);

public:
	static void setFileName(const QString &fileName);
	static QString fileName();

#ifndef __NO_TEXTCODEC
	static void setIniCodec(QTextCodec *codec);
	static void setIniCodec(const char *codecName);
	static QTextCodec *iniCodec();
#endif

private:
	JSettingsPrivate *d_ptr;
};

template <typename T> inline
void JSettings::write(const QString &key, T &&value)
{
	write(key, QVariant{value});
}

template <typename T> inline
T JSettings::read(const QString &key, T defaultValue)
{
	return read(key, QVariant{defaultValue}).value<T>();
}

} //namespace QTE


#endif //JSETTINGS_H
